package com.lx.dao;

import com.lx.bean.FileTest;
import com.lx.tool.SqlInit;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class FileTestImpl implements FileTestDao{
    PreparedStatement statement;
    ResultSet resultSet;
    @Override
    public boolean addFile(FileTest file) {
        //1、创建数据库的连接对象
        SqlInit sqlInit=SqlInit.build();

        //2、获取数据库的连接
        Connection connection = sqlInit.getConnection();
        //编写sql语句
        String preSql="insert into filetest(uuid,file_size,file_type,file_name,createtime,file_directory) values(?,?,?,?,?,?)";
        boolean bool=false;
        try {
            //先要对sql语句进行预处理，再补全sql语句
            statement=connection.prepareStatement(preSql);
            statement.setString(1,file.getUuid());
            statement.setString(2,file.getFileSize());
            statement.setString(3,file.getFileType());
            statement.setString(4,file.getFileName());
            statement.setString(5,file.getCreateTime());
            statement.setString(6,file.getFileDirectory());
            //执行sql语句
            int i=statement.executeUpdate();
            //判断
            if (i == 1){
                bool=true;
            }else{
                bool=false;
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            if (statement !=null){
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (connection != null){
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }


        return bool;
    }

    @Override
    public List<FileTest> selectAllFile() {
        //创建数据库连接对象
        SqlInit sqlInit=SqlInit.build();

        //获取连接对象
        Connection connection = sqlInit.getConnection();

        //编写sql语句
        String preSql="select uuid,file_size,file_type,file_name,createtime,file_directory from filetest";
        //创建一个list集合，把我们查询到的数据放在集合中
        List<FileTest> fileList=new ArrayList<>();
        try {
            //对sql进行预处理
            statement=connection.prepareStatement(preSql);
            //执行sql语句
            resultSet = statement.executeQuery();
            //遍历输出
            while(resultSet.next()){
                FileTest file = new FileTest();
                file.setUuid(resultSet.getString("uuid"));
                file.setFileSize(resultSet.getString("file_size"));
                file.setFileType(resultSet.getString("file_type"));
                file.setFileName(resultSet.getString("file_name"));
                file.setCreateTime(resultSet.getString("createtime"));
                file.setFileDirectory(resultSet.getString("file_directory"));
                fileList.add(file);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            if (resultSet !=null){
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (statement != null){
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (connection != null){
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return fileList;
    }
}
